// 2711. 对角线上不同值的数量差
// AC: https://leetcode.cn/problems/difference-of-number-of-distinct-values-on-diagonals/submissions/615383642/

class Solution {
public:
    vector<vector<int>> differenceOfDistinctValues(vector<vector<int>>& grid) {
        int m = grid.size(),n = grid[0].size();
        vector<vector<int>> ans(m,vector<int>(n));
        for(int i=0;i<m;++i){
            for(int j=0;j<n;++j){
                set<int> s;
                int x=i+1,y=j+1;
                while(x<m&&y<n){
                    s.insert(grid[x][y]);
                    x+=1;
                    y+=1;
                }
                set<int> s1;
                x=i-1,y=j-1;
                while(x>=0&&y>=0){
                    s1.insert(grid[x][y]);
                    x-=1;
                    y-=1;
                }
                ans[i][j]=abs((int)s.size()-(int)s1.size());
            }
        }
        return ans;
    }
};